<?php

namespace app\admin\model\evaluation;

use PhpOffice\PhpSpreadsheet\Calculation\Exception;
use think\Db;
use think\Model;


class Evaluation extends Model
{

    

    

    // 表名
    protected $name = 'evaluation';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'Scalar_id_text'
    ];


    public function getScalarIdList()
    {
        return ['7' =>7, '21' => 21, '28' =>28, '50' =>50, '52' =>52, '54' =>54, '73' =>73, '83' =>83, '84' =>84, '85' =>85];
    }
    public function getScalarIdTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['Scalar_id']) ? $data['Scalar_id'] : '');
        $list = $this->getScalarIdList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function input_csv($handle)
    {
        $out = array();
        $n =$m= 0;
        while ($data = fgetcsv($handle)) {
            $num = count($data);
            for ($i = 0; $i < $num; $i++) {
                if (!empty($data[$i])) {
//                    $data[$i] = mb_convert_encoding(trim($data[$i]), "UTF-8", "GBK");
                    $data[$i] = iconv(mb_detect_encoding(trim($data[$i])
                        , array("ASCII", 'UTF-8', "GB2312", "GBK", 'BIG5')), 'utf-8//IGNORE', trim($data[$i]));
                    $out[$n][$i] = $data[$i];
                } else {
                    $out[$n][$i] = "";
                }
            }
            if(count($out)>=10){
                $m += $this->importCSV($out);
                $out=[];
                $out[0] = ['类别','测评名称','量表id','测评简介','使用对象','年龄段（月）','题目数量', '测评时长','价格'];
            }
            $n++;
        }
        if(count($out)>0){
            $m += $this->importCSV($out);
        }
        return $m;
    }
    public function importCSV($result){
        unset($result[0]);
        $num = 0;
        foreach ($result as $val){

            $info['typename'] = $val[1];
            $info['name'] = $val[2];
            if(empty($info['name'])){
                continue;
            }
            $info['scalar_id'] = $val[3];
            $info['introduce'] = $val[4];
            $info['tag'] = $val[5];
            $info['age'] = $val[6];
            $info['number'] = $val[7];
            $info['timelong'] = $val[8];
            $info['price'] = $val[9];
            if($info['price'] <= 0){
                continue;
            }
            $info['createtime'] = time();
            Db::startTrans();
            try {
                //入库
                Db::name('evaluation')->insert($info);
                Db::commit();
                $num++;
            }catch (Exception $e){
                Db::rollback();
                return false;
            }
        }
        return $num;
    }





}
